package web.type;

import bean.Type;
import bean.Video;
import com.alibaba.fastjson.JSON;
import entity.TypeEntity;
import util.FANREN;

import javax.persistence.EntityManager;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

@WebServlet(name = "TypeEdit",urlPatterns = "/dao/update/typeEdit")
public class TypeEdit extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String jsonString = FANREN.getJson(request);
        Type type = JSON.parseObject(jsonString,Type.class);
        EntityManager em = (EntityManager) request.getAttribute("_entity.Manager");
        TypeEntity typeEntity = null;
        if(type.getTid().equals("")) {
            //新增
            typeEntity = new TypeEntity();
        } else {
            //修改
            typeEntity = em.find(TypeEntity.class,Integer.parseInt(type.getTid()));
        }
        typeEntity.setName(type.getName());
        em.persist(typeEntity);
    }
}
